<template>
  <div class="wjmm">
    <van-nav-bar title="忘记密码" left-arrow @click-left="$router.back()" />

    <van-form @submit="onSubmit" ref="regForm">
      <van-field
        left-icon="gem"
        v-model="username"
        name="username"
        label="手机号"
        placeholder="手机号"
        :rules="[
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
        ]"
      />

      <van-field
        left-icon="gem"
        v-model="vercode"
        name="vercode"
        clearable
        label="验证码"
        placeholder="验证码"
        :rules="[
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码错误' },
        ]"
      >
        <template #button>
          <van-button
            size="mini"
            @click="cend"
            native-type="button"
            :disabled="btnDisabled"
            >{{ codText }}</van-button
          >
        </template>
      </van-field>

      <van-field
        left-icon="gem"
        v-model="password"
        type="password"
        name="password"
        label="新密码"
        placeholder="新密码"
        :rules="[
          { required: true, message: '请输入密码' },
          {
            validator: (e) => /^\w{6,12}$/.test(e),
            message: '密码格式为6-12位字母或数字',
          },
        ]"
      />
      <van-button
        color="#4fc08d"
        block
        
        native-type="submit"
        :loading="submitLoading"
        >确定重置</van-button
      >
    </van-form>
  </div>
</template>

<script>
import { mima } from "@/api/wjmm";
import { sendSms } from "@/api/zhuce";
export default {
  name: "wjmm",
  data() {
    return {
      username: "",
      password: "",
      vercode: "",
      btnDisabled: false,
      codText: "发送验证码",
      submitLoading: false,
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      this.submitLoading = true;
      mima(values)
        .then((res) => {
          console.log(res);
        })
        .finally((e) => {
          this.submitLoading = false;
        });
      setTimeout((v) => {
        this.$router.push('Login');
      }, 800);
    },
    async cend() {
      try {
        this.$refs.regForm.validate("username"); //
        //

        this.btnDisabled = true; //不可点击

        this.codText = "发送中...";
        let timer = null;
        let count = 10;

        let res = await sendSms({
          mobile: this.username,
          type: "login",
        })
          .then((res) => {
            if (res.code == 0) {
              this.$toast(`验证码已经发送到${this.username}请注意查收`);

              timer = setInterval((e) => {
                count--;
                this.codText = `${count}s后再次发送`;
                if (count <= 0) {
                  clearInterval(timer);
                  count = 10;
                  this.codText = "再次获取";
                  this.btnDisabled = false;
                }
              }, 1000);
            } else {
              this.$toast(res.msg);
              this.btnDisabled = false; //不可点击

              this.codText = "再次发送";
            }
          })
          .catch((err) => {
            //提示失败  再次发送  恢复到可以点击的状态

            this.$toast(res.msg || "网络错误");

            this.btnDisabled = false; //不可点击

            this.codText = "再次发送";
          });
      } catch (error) {}
    },
  },
};
</script>

<style></style>
